package com.baizhi.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
//强制登陆过滤器
public class LoginFilter implements Filter {

	@Override
	public void destroy() {
		// TODO Auto-generated method stub

	}

	@Override
	public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain)
			throws IOException, ServletException {
//		1、获取session中的登陆成功标识
		HttpServletRequest hreq = (HttpServletRequest)req;
		HttpSession session = hreq.getSession();
		Object o = session.getAttribute("login");
//		2、判断是否放行
		if(o!=null) {
			chain.doFilter(req, resp);
		}else {
			HttpServletResponse hresp = (HttpServletResponse)resp;
			hresp.sendRedirect(hreq.getContextPath()+"/login.jsp");
		}
			

	}

	@Override
	public void init(FilterConfig arg0) throws ServletException {
		// TODO Auto-generated method stub

	}

}
